package com.foxmail.aroundme.boring.module.news.main.module;

import android.support.annotation.NonNull;

import com.foxmail.aroundme.boring.bean.newslist.NewsListBean;
import com.foxmail.aroundme.boring.callback.RequestCallback;
import com.foxmail.aroundme.boring.manager.NetManager;
import com.foxmail.aroundme.boring.module.news.main.presenter.INewsListPre;
import com.foxmail.aroundme.boring.utils.Config;
import com.foxmail.aroundme.boring.utils.URL;
import com.foxmail.aroundme.boring.utils.m_enum.RefreshType;

import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.functions.Action0;
import rx.schedulers.Schedulers;

/**
 * Created by gzl on 1/22/17.
 *
 */

public class NewsListInterImpl implements INewsListInter<List<NewsListBean>> {

    private INewsListPre iNewsListPre;

    public NewsListInterImpl(INewsListPre iNewsListPre) {
        this.iNewsListPre = iNewsListPre;
    }

    //默认从1开始
    private int pageIndex = 1;

    private int pageCount = 10;

    @Override
    public Subscription getDataFromNet(@NonNull final RequestCallback<List<NewsListBean>> callback, @RefreshType.Refresh int type,
                                       @NonNull final String unixTime, @NonNull final String channelId, final boolean isRefresh) {

        pageIndex += 1;
        //如果是刷新的话置为1
        if (isRefresh) {
            pageIndex = 1;
        }

        return Observable.create(new Observable.OnSubscribe<List<NewsListBean>>() {
            @Override
            public void call(Subscriber<? super List<NewsListBean>> subscriber) {
                //刷新
                if (isRefresh) {
                    NetManager.newsInstance().GetNewsFromNet(callback, channelId, pageIndex, pageCount, null, null);
                } else {
                    //加载
                    NetManager.newsInstance().GetNewsFromNet(callback, channelId, pageIndex, pageCount, Config.sort_desc, unixTime);
                }
            }
        }).doOnSubscribe(new Action0() {
            @Override
            public void call() {

            }
        }).subscribeOn(Schedulers.newThread()).subscribe(new Subscriber<List<NewsListBean>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(List<NewsListBean> jokes) {

            }
        });
    }
}
